package dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import po.Customer;

public class CustomerDao implements ICustomerDao{
	public boolean insertCustomer(Customer cus){	
		Session session = util.HibernateSessionFactory.getSession();
		Transaction tran = null;
		try{
			tran = session.beginTransaction();
			session.save(cus);
			tran.commit();
			return true;			
		}catch(Exception ex){
			tran.rollback();
			ex.printStackTrace();
			}
		finally{
			util.HibernateSessionFactory.closeSession();
		}
		return false;
	} 	
	
	public boolean updateCustomer(Customer cus){
		Session session = util.HibernateSessionFactory.getSession();
		Transaction tran = null;
		try{
			tran = session.beginTransaction();
			session.update(cus);
			tran.commit();
			return true;			
		}catch(Exception ex){
			tran.rollback();
			ex.printStackTrace();
			}
		finally{
			util.HibernateSessionFactory.closeSession();
		}
		return false;
	} 	
	
	public Customer getCustomerByAccount(String account){
		Session session = util.HibernateSessionFactory.getSession();
		Customer cus = null;
		try{
			cus = (Customer)session.get(Customer.class, account);
		}catch(Exception ex){
			ex.printStackTrace();
			}
		finally{
			util.HibernateSessionFactory.closeSession();
		}
		return cus;
	} 	
}
